package com.xc.ezplan.admin.controller;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.log.annotation.Log;
import com.xc.ezplan.log.entity.AdminLog;
import com.xc.ezplan.log.service.AdminLogService;
import com.xc.ezplan.log.vo.AdminLogVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @author lixingchen
 * @date 2022/11/20
 * @description
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/log")
@Api(tags = "日志管理")
public class AdminLogController {

	private final AdminLogService adminLogService;

	@ApiOperation("按条件分页查询日志")
	@PostMapping("/page")
	public EzResult<Object> pageQueryLog(@RequestBody BaseSearchBody<AdminLog> baseSearchBody) {
		return EzResult.ok(adminLogService.pageQueryLog(baseSearchBody.getSearchCondition(), baseSearchBody.getPage()));
	}

	@ApiOperation("按用户分页查询日志")
	@PostMapping("/user/page")
	public EzResult<Object> pageQueryUserLog(@RequestBody Page<AdminLog> page) {
		return EzResult.ok(adminLogService.pageQueryUserLog(page));
	}

	@DeleteMapping
	@Log("删除日志")
	@ApiOperation("删除日志")
	public EzResult<Object> deleteLog(@RequestParam Long id) {
		adminLogService.deleteLog(id);
		return EzResult.ok();
	}

	@GetMapping(value = "/error")
	@ApiOperation("错误日志查询")
	public EzResult<Object> queryErrorLog(AdminLogVo adminLogVo, Page<AdminLog> page) {
		adminLogVo.setLogType("ERROR");
		return null;
	}

	@GetMapping(value = "/error/{id}")
	@ApiOperation("日志异常详情查询")
	public EzResult<Object> queryErrorLogDetail(@PathVariable Long id) {
		return EzResult.ok(adminLogService.queryErrorLogDetail(id));
	}

	@DeleteMapping("/del/info")
	@Log("删除所有INFO日志")
	@ApiOperation("删除所有INFO日志")
	@SaCheckPermission("admin.log.info.delete")
	public EzResult<Object> delAllInfoLog() {
		String logType = "INFO";
		adminLogService.deleteAllLog(logType);
		return EzResult.ok();
	}

	@DeleteMapping("/del/error")
	@Log("删除所有ERROR日志")
	@ApiOperation("删除所有ERROR日志")
	@SaCheckPermission("admin.log.error.delete")
	public EzResult<Object> delAllErrorLog() {
		String logType = "ERROR";
		adminLogService.deleteAllLog(logType);
		return EzResult.ok();
	}

	@Log("导出正常数据")
	@ApiOperation("导出正常数据")
	@GetMapping(value = "/download")
	public void exportLog(HttpServletResponse response) throws IOException {

	}

	@Log("导出错误数据")
	@ApiOperation("导出错误数据")
	@GetMapping(value = "/error/download")
	public void exportErrorLog(HttpServletResponse response) throws IOException {

	}
}
